package com.kgc.service.impl;

import com.kgc.dao.MenuInfoDao;
import com.kgc.dao.RoleDao;
import com.kgc.dao.UserInfoDao;
import com.kgc.entity.MenuInfo;
import com.kgc.entity.Role;
import com.kgc.entity.vo.MenuInfoVo;
import com.kgc.entity.vo.UserInfoVo;
import com.kgc.service.MenuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuInfoServiceImpl implements MenuInfoService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuInfoDao menuInfoDao;
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public Map<String,Object> getMenuInfo(Integer userId) {

        //根据用户ID查询其角色
        Role role = roleDao.getRoleByUserId(userId);
        List<MenuInfoVo> oneMenuInfo = menuInfoDao.getOneMenuInfo(role.getId());
        for (MenuInfoVo menuInfoVo : oneMenuInfo) {
            Map<String,Object> map=new HashMap<>();
            map.put("roleId",role.getId());
            map.put("pId",menuInfoVo.getId());
            List<MenuInfo> twoMenuInfo = menuInfoDao.getTwoMenuInfo(map);
            menuInfoVo.setChild(twoMenuInfo);
        }

        Map<String,Object> map=new HashMap<>();
        //welcome 主页配置
        Map<String,Object> welcome=new HashMap<>();
        welcome.put("title","首页");
        welcome.put("href","/welcome");

        map.put("homeInfo",welcome);

        Map<String,Object> logo=new HashMap<>();
        logo.put("title","管理系统");
        logo.put("image","images/logo.png");
        logo.put("href","");

        map.put("logoInfo",logo);

        map.put("menuInfo",oneMenuInfo);

        return map;
    }

    @Override
    public List<MenuInfo> getMenuInfoAll() {
        return menuInfoDao.getMenuInfoAll();
    }

    @Override
    public int delMenuInfoById(Integer menuId,Integer status) {
        return menuInfoDao.delMenuInfoById(menuId,status);
    }

    @Override
    public List<MenuInfo> getMenuInfoOne() {
        return menuInfoDao.getMenuInfoOne();
    }

    @Override
    public int addMenuInfo(MenuInfo menuInfo) {
        return menuInfoDao.addMenuInfo(menuInfo);
    }

    @Override
    public MenuInfo getMenuInfoById(Integer id) {
        return menuInfoDao.getMenuInfoById(id);
    }

    @Override
    public int updateMenuInfo(MenuInfo menuInfo) {
        return menuInfoDao.updateMenuInfo(menuInfo);
    }

    @Override
    public int countMenuByPId(Integer menuId) {
        return menuInfoDao.countMenuByPId(menuId);
    }

    @Override
    public List<Map<String, Object>> getMenuTree(Integer roleId) {
        //1.查询所有菜单
        List<Map<String, Object>> menuInfoOne = menuInfoDao.getMenuInfo(null,null);
        //查询当前角色 所绑定的一级菜单
        List<Map<String, Object>> menuInfoOne1 = menuInfoDao.getMenuInfo(null,roleId);
        //当前角色所拥有的二级菜单
//        List<Map<String, Object>> menuInfos2=new ArrayList<>();
        for (Map<String, Object> map : menuInfoOne) {
            map.put("spread",true);//默然展开
            for (Map<String, Object> stringObjectMap : menuInfoOne1) {
                if ((Integer)map.get("id")==(Integer)stringObjectMap.get("id")&&map.get("id")!=null){
                    map.put("checked",true);
                }
            }
            List<Map<String, Object>> menuInfos= menuInfoDao.getMenuInfo((Integer) map.get("id"),null);
            List<Map<String, Object>> menuInfos2= menuInfoDao.getMenuInfo((Integer) map.get("id"),roleId);
            for (Map<String, Object> menuInfo : menuInfos) {
                for (Map<String, Object> stringObjectMap : menuInfos2) {
                    if ((Integer)stringObjectMap.get("id")==(Integer)menuInfo.get("id")||menuInfo.get("id")!=null){
                        menuInfo.put("checked",true);
                    }
                }
            }
            map.put("children",menuInfos);
        }
        //2.查询当前角色已经绑定的菜单
        //3.遍历 让已绑定的菜单 加上 选中属性
        return menuInfoOne;
    }

    @Override
    public List<Map<String, Object>> getCMenuTree(Integer userId) {

        //1.查询主账号菜单
        UserInfoVo user = userInfoDao.getUserById(userId);

        List<Map<String, Object>> menuInfoOne = menuInfoDao.getMenuInfo(null,user.getRoleId());
        //查询当前角色 所绑定的一级菜单
        List<Map<String, Object>> menuInfoOne1 = menuInfoDao.getCMenuInfo(null,userId);
        //当前角色所拥有的二级菜单
//        List<Map<String, Object>> menuInfos2=new ArrayList<>();
        for (Map<String, Object> map : menuInfoOne) {
            map.put("spread",true);//默然展开
            for (Map<String, Object> stringObjectMap : menuInfoOne1) {
                if ((Integer)map.get("id")==(Integer)stringObjectMap.get("id")&&map.get("id")!=null){
                    map.put("checked",true);
                }
            }
            List<Map<String, Object>> menuInfos= menuInfoDao.getMenuInfo((Integer) map.get("id"),user.getRoleId());
            List<Map<String, Object>> menuInfos2= menuInfoDao.getCMenuInfo((Integer) map.get("id"),userId);
            for (Map<String, Object> menuInfo : menuInfos) {
                for (Map<String, Object> stringObjectMap : menuInfos2) {
                    if ((Integer)stringObjectMap.get("id")==(Integer)menuInfo.get("id")||menuInfo.get("id")!=null){
                        menuInfo.put("checked",true);
                    }
                }
            }
            map.put("children",menuInfos);
        }
        return menuInfoOne;
    }
}
